package com.sprouting.personal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.personal.dao.HomeworkDao;
import com.sprouting.personal.dao.HomeworkRecordDao;
import com.sprouting.personal.model.entity.Homework;
import com.sprouting.personal.model.entity.HomeworkRecord;
import com.sprouting.personal.model.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 家庭作业 service接口实现
 *
 * @author ：sprouting
 * @date 创建时间： 2023-07-15 10:36:32
 */
@Service
@Slf4j
public class HomeworkService extends ServiceImpl<HomeworkDao, Homework> {

	@Resource
	private HomeworkDao homeworkDao;
	@Resource
	private HomeworkRecordDao homeworkRecordDao;


	/**
	 * 通过ID查询单条数据
	 * @param id 主键
	 * @return 家庭作业
	 */
	public Homework getHomeworkById(String id) {
		return homeworkDao.getHomeworkById(id);
	}

	/**
	 * 分页查询
	 * @param page 分页参数
	 * @param homework 查询参数
	 * @return 条件查询结果
	 */
	public Page<HomeworkVo> getHomeworkPage(Page page, Homework homework){
		return homeworkDao.getHomeworkPage(page, homework);
	}

	/**
	 * 根据家庭作业ID删除
	 * @param id 家庭作业ID
	 */
	@Transactional(rollbackFor = Exception.class)
	public void delHomeworkById(String id) {
		homeworkDao.deleteById(id);

		LambdaQueryWrapper<HomeworkRecord> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(HomeworkRecord::getHomeworkId, id);
		homeworkRecordDao.delete(wrapper);
	}

	/**
	 * 根据家庭成员查询 未完成的作业
	 * @param memberFamilyId 家庭成员ID
	 * @return
	 */
	public List<HomeworkVo> getIncompleteHomeworkByMemberFamilyId(String memberFamilyId) {
		return homeworkDao.getIncompleteHomeworkByMemberFamilyId(memberFamilyId);
	}

	/**
	 * 作业签收
	 * @param thisExecTime 本次执行的时间
	 * @param homeworkId 作业ID
	 * @param homeworkRecord 作业详情
	 * @param checkHomework 作业是否待检查
	 */
	@Transactional(rollbackFor = Exception.class)
	public void homeworkCompletedNormal(Date thisExecTime, String homeworkId, Integer checkHomework, HomeworkRecord homeworkRecord) {
		// 注意，只更新执行时间，其他的都不动
		LambdaUpdateWrapper<Homework> wrapper = new LambdaUpdateWrapper<>();
		wrapper.eq(Homework::getId, homeworkId);
		wrapper.set(Homework::getExecTime, thisExecTime);
		wrapper.set(checkHomework != null, Homework::getCheckHomework, checkHomework);
		homeworkDao.update(null, wrapper);

		homeworkRecordDao.insert(homeworkRecord);
	}

	/**
	 * 获取今天已完成的作业
	 * @param memberFamilyId 家庭成员ID
	 * @param now 查询的时间
	 * @return
	 */
	public List<HomeworkVo> getHomeworkCompletedToday(String memberFamilyId, Date now) {
		return homeworkDao.getHomeworkCompletedToday(memberFamilyId, now);
	}

	/**
	 * 获取未检查的作业
	 * @param memberFamilyId 家庭成员ID
	 * @return
	 */
	public List<HomeworkVo> getUncheckedHomework(String memberFamilyId) {
		return homeworkDao.getUncheckedHomework(memberFamilyId);
	}

	/**
	 * 统计作业数据
	 * @param memberFamilyId 家庭成员ID
	 * @return
	 */
	public HomeworkStatistics statistics(String memberFamilyId) {
		return homeworkDao.statistics(memberFamilyId);
	}

	/**
	 * 获取学科作业数量和名称
	 * @param memberFamilyId 家庭成员id
	 * @return
	 */
	public List<DisciplinesVo> getDisciplinesNameNum(String memberFamilyId) {
		return homeworkDao.getDisciplinesNameNum(memberFamilyId);
	}

	/**
	 * 根据家庭成员ID获取最近执行的100条家庭作业记录
	 * @param memberFamilyId 家庭成员
	 * @return
	 */
	public List<HomeworkVo> getLast100Log(String memberFamilyId) {
		return homeworkDao.getLast100Log(memberFamilyId);
	}

	/**
	 * 根据家庭成员ID 获取完成次数做多的作业10条
	 * @param memberFamilyId 家庭成员ID
	 * @return
	 */
	public List<CompleteTheMostHomework> getCompleteTheMostTask(String memberFamilyId) {
		return homeworkDao.getCompleteTheMostTask(memberFamilyId);
	}

}
